--函数！

--用法：local HX = require "packages/konglong/hxfs2"
--如HX.DrawCardFromPile(player.room, player, baguan2022.name, 2)

local hxfs2 = {}


--选择一种类型的牌，从牌堆中依次摸任意张
---@param room Room @ 房间
---@param player ServerPlayer @ 摸牌的角色
---@param Skill string @ 技能名
---@param num integer @ 要摸的数量
function hxfs2.DrawCardFromPile(room, player, Skill, num)
    local choice = room:askToChoice(player,{
        choices = {"基本牌", "锦囊牌", "装备牌"},
        skill_name = Skill,
        prompt = "请选择摸的牌的类型",
    })
    local cardType = ""
    if choice == "基本牌" then
        cardType = "basic"
    elseif choice == "锦囊牌" then
        cardType = "trick"
    elseif choice == "装备牌" then
        cardType = "equip"
    else
        return -- 如果选择无效，直接返回
    end
    for i = 1, num do
      local cards = room:getCardsFromPileByRule(".|.|.|.|.|" .. cardType)
      if #cards > 0 then
        room:obtainCard(player, cards[1], false, fk.ReasonDraw, player, Skill)
      end
    end
end


---通过仅改变武将改变武将图像
---@param room Room @ 房间
---@param player ServerPlayer 改变图像的角色
---@param old string   需要改变的武将名
---@param new string   改变后的武将名
function hxfs2.ChangeGeneral(room, player, old, new)
  if player.deputyGeneral == old or player.general == old then
    if player.deputyGeneral == old then
      player.deputyGeneral = new
      room:broadcastProperty(player, "deputyGeneral")
    elseif player.general == old then
      player.general = new
      room:broadcastProperty(player, "general")
    end
  else
    player.general = new
    room:broadcastProperty(player, "general")
  end
end

---设置一名角色所有技能的已发动次数（自动发送战报）
---@param player ServerPlayer @ 角色
---@param times integer @ 要设置的次数
function hxfs2.SetSkillsTimes(player, times)
  local skills = table.simpleClone(Fk.generals[player.general]:getSkillNameList(true))
  if player.deputyGeneral ~= "" then
    table.insertTableIfNeed(skills, Fk.generals[player.deputyGeneral]:getSkillNameList(true))
  end
  skills = table.filter(skills, function(s) return player:hasSkill(s) end)
  if #skills > 0 then
    for _, s in ipairs(skills) do
      player:setSkillUseHistory(s, times, Player.HistoryPhase)
      player:setSkillUseHistory(s, times, Player.HistoryTurn)
      player:setSkillUseHistory(s, times, Player.HistoryRound)
      player:setSkillUseHistory(s, times, Player.HistoryGame)
    end
    player.room:sendLog{
      type = "%from 所有技能的发动次数已被设置为 "..times.." 次！",
      from = player.id,
      --arg = s,
    }
  end
end


---令一名角色将手牌数摸至或弃至对应值
---@param room Room @ 房间
---@param player ServerPlayer @ 角色
---@param num integer @ 值
---@param Skill string @ 技能名
function hxfs2.SetPlayerHandNum(room, player, num, Skill)
  local n = player:getHandcardNum()
  if n < num then
    player:drawCards(num-n, Skill)
  elseif n > num then
    room:askToDiscard(player, {
      min_num = n-num,
      max_num = n-num,
      include_equip = false,
      skill_name = Skill,
      cancelable = false,
    })
  end
end


---令一名角色从牌堆中获得一张指定牌名的牌（牌堆没有会从弃牌堆中获得）
---@param room Room @ 房间
---@param player ServerPlayer @ 角色
---@param name string @ 牌名
---@param Skill string @ 技能名
---@param moveMark? table|string @ 移动后自动赋予标记，格式：{标记名(支持-inarea后缀，移出值代表区域后清除), 值}
function hxfs2.GetCardFromPile(room, player, name, Skill, moveMark)
  local card1 = table.filter(room.draw_pile, function(id)
    return Fk:getCardById(id).trueName == name
  end)
  if #card1 > 0 then
    local card = Fk:getCardById(table.random(card1))
    room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill, moveMark)
  else
    local card1_2 = table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).trueName == name
    end)
    if #card1_2 > 0 then
      local card = Fk:getCardById(table.random(card1_2))
      room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill, moveMark)
    end
  end
end


---令一名角色从牌堆中获得一张指定点数的牌（牌堆没有会从弃牌堆中获得）
---@param room Room @ 房间
---@param player ServerPlayer @ 角色
---@param number string @ 点数
---@param Skill string @ 技能名
function hxfs2.GetNumberCardFromPile(room, player, number, Skill)
  local card1 = table.filter(room.draw_pile, function(id)
    return Fk:getCardById(id).number == number
  end)
  if #card1 > 0 then
    local card = Fk:getCardById(table.random(card1))
    room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
  else
    local card1_2 = table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).number == number
    end)
    if #card1_2 > 0 then
      local card = Fk:getCardById(table.random(card1_2))
      room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
    end
  end
end


---令一名角色从牌堆中获得一张指定花色的牌（牌堆没有会从弃牌堆中获得）
---@param room Room @ 房间
---@param player ServerPlayer @ 角色
---@param suit string @ 花色
---@param Skill string @ 技能名
function hxfs2.GetSuitCardFromPile(room, player, suit, Skill)
  local card1 = table.filter(room.draw_pile, function(id)
    return Fk:getCardById(id).suit == suit
  end)
  if #card1 > 0 then
    local card = Fk:getCardById(table.random(card1))
    room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
  else
    local card1_2 = table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).suit == suit
    end)
    if #card1_2 > 0 then
      local card = Fk:getCardById(table.random(card1_2))
      room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
    end
  end
end


---令一名角色从牌堆中获得一张指定类型的牌（牌堆没有会从弃牌堆中获得）
---@param room Room @ 房间
---@param player ServerPlayer @ 角色
---@param type string @ 类型
---@param Skill string @ 技能名
function hxfs2.GetTypeCardFromPile(room, player, type, Skill)
  local card1 = table.filter(room.draw_pile, function(id)
    return Fk:getCardById(id).type == type
  end)
  if #card1 > 0 then
    local card = Fk:getCardById(table.random(card1))
    room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
  else
    local card1_2 = table.filter(room.discard_pile, function(id)
      return Fk:getCardById(id).type == type
    end)
    if #card1_2 > 0 then
      local card = Fk:getCardById(table.random(card1_2))
      room:obtainCard(player, card, false, fk.ReasonPrey, player, Skill)
    end
  end
end

return hxfs2